<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="appRoot" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <base href="${appRoot}/"/>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script>
        $(function () {
            if (window.WebSocket) {
                let msg = "";
                let webSocket = new WebSocket(encodeURI("ws://" + document.location.host + "/mybatis-spring/websocket"));
                webSocket.onopen = function () {
                    console.log("WebSocket连接成功");
                };
                webSocket.onerror = function () {
                    console.log("WebSocket错误");
                };
                webSocket.onclose = function () {
                    console.log("WebSocket连接断开");
                };
                //消息接收
                webSocket.onmessage = function (message) {
                    msg+=message.data;
                    $("#divMsg").text(msg);
                };
                $("#btnSend").click(function () {
                    webSocket.send($("#txtMessage").val());
                });
            }
        });
    </script>
</head>
<body>
WebSocket测试
<input id="txtMessage" type="text"/><input id="btnSend" type="button" value="发送"/>
<div id="divMsg"></div>
</body>
</html>
